package com.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.e.BaseServlet;

import db.DBManager;
import db.QueryDataResult;
import com.entity.ProfileEntity;
import com.entity.UserBean;
import com.entity.UserEntity;

/**
 * Servlet implementation class UserServlet2
 */
public class ProfileServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
       
	UserEntity dao=new UserEntity();
	public void profilelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		ArrayList<ProfileEntity> alist = new ArrayList<ProfileEntity>(); 
		String sqlString="select id,name,birthday,gender,career,address,moblie from profile";
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						alist.add(new ProfileEntity(rsSet.getString("id"),rsSet.getString("name"),rsSet.getString("birthday")
								,rsSet.getString("gender"),rsSet.getString("career"),rsSet.getString("address"),
								rsSet.getString("moblie")));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
			request.setAttribute("us", alist); 
		    request.getRequestDispatcher("list.jsp").forward(request, response); 
	}

	public void profileDetail(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String idString = request.getParameter("id");
		// sql
		ProfileEntity profileEntity = null;
		String sqlString = "select id,name,birthday,gender,career,address,moblie from profile where id ='"+idString+"'";
		// 执行 sql
		QueryDataResult qResult = DBManager.excuteQuery(sqlString);
		// 处理sql
		if(qResult!=null) {
			while(qResult.rs.next()) {
				profileEntity = new ProfileEntity(
					qResult.rs.getString("id"),
					qResult.rs.getString("name"),
					qResult.rs.getString("birthday"),
					qResult.rs.getString("gender"),
					qResult.rs.getString("career"),
					qResult.rs.getString("address"),
					qResult.rs.getString("moblie"));
			}
		}
		qResult.close();
		
		request.setAttribute("profileEntity",profileEntity);
		request.getRequestDispatcher("user.jsp").forward(request, response);
		
	}
	
	public void profileAdd(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String sqlString = "insert profile(id,name) value ('"+id+"','"+name+"')";
		int result = DBManager.excuteUpdate(sqlString);
		if(result>0) {
			request.getRequestDispatcher("ProfileServlet?reqName=profilelist").forward(request, response);
		}else {
			request.setAttribute("msg","添加失败");
			request.getRequestDispatcher("profileAdd.jsp").forward(request, response);
		}
	}
	public void jstl1(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		request.setAttribute("str","str");
		request.setAttribute("str1",null);
		request.getRequestDispatcher("list1.jsp").forward(request, response);
		
	}
	public void jstl2(HttpServletRequest request,HttpServletResponse response) throws Exception {
			
			request.setAttribute("num1",2);
			request.setAttribute("num2",10);
			request.setAttribute("v1", "");
			request.setAttribute("v2", null);
			request.getRequestDispatcher("list1.jsp").forward(request, response);
			
	}
	public void selectALL(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String keyName=request.getParameter("name");
		ArrayList<UserBean> list=dao.selectALL(keyName);
		  request.setAttribute("alist", list);
		request.getRequestDispatcher("list2.jsp").forward(request, response);
		
   }
	public void userDetail(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		UserBean list=dao.userDetail(id);
		  request.setAttribute("user", list);
		request.getRequestDispatcher("userDetail.jsp").forward(request, response);
		
   }
	public void addUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String sex=request.getParameter("sex");
			String age=request.getParameter("age");
			int result=dao.addUser(new UserBean(name, age, sex, id));
			if (result>0) {
				//request.getRequestDispatcher("ProfileServlet?reqName=selectall").forward(request, response);
				response.sendRedirect("ProfileServlet?reqName=selectALL");
			}else {
				request.setAttribute("msg", "添加失败");
				request.getRequestDispatcher("userAdd.jsp").forward(request, response);
			}
	}
	public void updateUser(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		String age=request.getParameter("age");
		int result=dao.updateUser(new UserBean(name, age, sex, id));
			if (result>0) {
				//request.getRequestDispatcher("ProfileServlet?reqName=selectall").forward(request, response);
				response.sendRedirect("ProfileServlet?reqName=selectALL");
			}else {
				request.setAttribute("result", "修改失败，请重新修改");
				request.getRequestDispatcher("ProfileServlet?reqName=userDetail").forward(request, response);
			}
        }
	public void userDelete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String id=request.getParameter("id");
		int result=dao.deleteUser(id);
		if (result>0) {
			request.setAttribute("msg", "删除成功");
		}else {
			request.setAttribute("msg", "删除失败");
		}
		request.getRequestDispatcher("ProfileServlet?reqName=selectALL").forward(request, response);
        }
}
